<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Feedback extends CI_Controller {
	var $perpage = 20;

		public function __construct() {
		   parent::__construct();
		   if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()) redirect('auth/login/'.str_replace("/", ".",$_SERVER['REQUEST_URI']));
		   $this->layout->setlayout('admin/layout');
		   $this->crumps->push('/admin/feedback/', 'Обратная связь');
		   $this->users_rights_model->check_rights(strtolower(get_class()), $this->router->get_method());
	   }

	public function index()	{

		$data['from'] = $from = $this->input->get('from', true);
		if ($from == "") $from = 0;
		$data['pages'] = $pages = $this->db->select()->from('feedback')->limit($this->perpage, $from)->order_by('feedback_date', 'desc')->get()->result_array();

		/* pagination */
		$count = $this->db->select()->from('feedback')->count_all_results();
		if ($from != 0 && sizeof($pages) <= 0 && $count > 0) {
			$pgng = $from - $this->perpage;
			redirect('admin/feedback/index/?from='.$pgng);
		}
		$this->load->library('pagination');
		$config['base_url'] = '/admin/feedback/index/';
		$config['total_rows'] = $count;
		$config['page_query_string'] = TRUE;
		$config['per_page'] = $this->perpage;
		$this->pagination->initialize($config);
		$data['paging'] = $this->pagination->create_links();
		/* pagination */

		$this->layout->view('admin/feedback/index', $data);
	}
	public function show($id = 0) {
		$this->crumps->push('/admin/pg/'.($id == 0?'add':'edit/'.$id).'/', "Просмотр");
		if ($id != 0) $data['page'] = $page = $this->db->select()->from('feedback')->where('feedback_id', $id)->get()->row_array();
		if ($id != 0 && sizeof($page) <= 0) redirect('admin/feedback');
		$this->layout->view('admin/feedback/show', $data);
	}
	public function delete($id = 0) {
		$from = $this->input->get('from', true);
		if ($from != "" && is_numeric($from)) $pgng = "?from=".$from;
		if ($id != 0) $data['page'] = $page = $this->db->select()->from('feedback')->where('feedback_id', $id)->get()->result_array();
		if ($id != 0 && sizeof($page) <= 0) redirect('admin/feedback');
		$this->db->where('feedback_id', $id)->delete('feedback');
		$this->session->set_flashdata('msg', 'Успешно удалено');
		redirect('admin/feedback/'.$pgng);
	}
}